# coding=utf-8
# author:xc
# datetime:2018/10/23 14:24

from . import api
from dbConnection.MysqlConn import Mysql
from app import util
from flask import request

data_location = [{'typeId': 1, 'name': "新闻定位"}, {'typeId': 2, 'name': "图片定位"}, {'typeId': 3, 'name': "视频定位"}]


@api.route('/api/news/type', methods=['POST', 'GET'])
def api_news_type():
    data = {
        'data': data_location
    }
    return util.to_json(data)


@api.route('/api/news/location', methods=['POST', 'GET'])
@util.verify_login
def api_news_location(uid):
    try:
        page = request.values.get('page', default=1)
        locateType = request.values.get('typeId')
        page = int(page)
        num = (page - 1) * 20
        mysql = Mysql()
        sql = "select * from news_location where uid='%s' and locateType='%s' order by addTime desc limit %d,%d"%(uid,locateType,num, 20)
        res = mysql.getAll(sql, None)
        mysql.dispose()
        code = util.CODE_EMPTY
        result = []
        if res:
            code = util.CODE_SUCCESS
            result = res
    except Exception as e:
        print e
        result = []
        code = util.CODE_ERROR
    data = {
        'code': code,
        'data': result

    }
    return util.to_json(data)


@api.route('/api/news/location_list', methods=['POST', 'GET'])
@util.verify_login
def api_news_location_list(uid):
    try:
        page = request.values.get('page', default=1)
        locateId = request.values.get('locateId')
        page = int(page)
        num = (page - 1) * 20
        mysql = Mysql()
        sql = "select * from news_location_type where locateId='%s' order by locateTime desc limit %d,%d" % (
            locateId, num, 20)
        res = mysql.getAll(sql, None)
        mysql.dispose()
        code = util.CODE_ERROR
        result = []
        if res:
            code = util.CODE_SUCCESS
            result = res
    except Exception as e:
        print e
        result = []
        code = util.CODE_ERROR
    data = {
        'code': code,
        'data': result

    }
    return util.to_json(data)
